package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"os"
	"strings"
	"time"
)

// CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app2 redis_clean_go_redis.go

var (
	client *redis.Client
)

func init() {
	client = redis.NewClient(&redis.Options{
		Network:   "",
		//Addr:      "127.0.0.1:6379",
		//Addr:      "r-bp1i6lg0wtd1g0z4jwpd.redis.rds.aliyuncs.com:6379", // 公网
		Addr:      "r-bp1i6lg0wtd1g0z4jw.redis.rds.aliyuncs.com:6379", // 内网
		Dialer:    nil,
		OnConnect: nil,
		//Password:  "", // no password set
		Password:  "Qcwl123456",
		DB:        0,  // use default DB
		//MaxRetries:         0,
		PoolSize:     10, // 线程池大小
		MinIdleConns: 0,  // 最小空闲链接,建立新连接
	})

	if pong, err := client.Ping().Result(); err != nil {
		fmt.Println(pong)
		panic(err)
	}
	// Output: PONG <nil>
}


func cleanDauData() {
	// 需要匹配的前缀
	// 删除的格式 : m::dau_new_vivo_20210103
	prefix := "m::dau_"

	var cursor uint64
	var n int
	for {
		var keys []string
		var err error
		// 每次取出 10个
		keys, cursor, err = client.Scan(cursor, prefix+"*", 10).Result()
		if err != nil {
			panic(err)
		}
		// 获取 2021-06-01
		month6, err := time.Parse("2006-01-02", "2021-10-17")
		if err != nil {
			panic(err)
		}
		// 判断到今天的秒数差
		monthDiff := time.Now().Unix() - month6.Unix()
		for _, value := range keys {
			fmt.Println("===== "+value)
			if strings.HasPrefix(value, prefix) {
				// 截取 key 里面的时间
				newStr := value[len(value) - 8:]
				year := newStr[:4]
				month := newStr[4:4+2]
				day := newStr[6:6+2]

				//dateStr := strings.Replace(value, prefix, "", -1)[0:10]
				dateStr := year+"-"+month+"-"+day
				//fmt.Println(dateStr)
				//return
				// 解析时间,并且清除指定时间之前的数据
				if timeObj, err := time.Parse("2006-01-02", dateStr); err == nil {
					//os.Stdout.WriteString(value+"\n")
					//fmt.Println(time.Now().Unix() - timeObj.Unix(),monthDiff, dateStr)
					if time.Now().Unix()-timeObj.Unix() > monthDiff {
						client.Del(value)
						os.Stdout.WriteString("del: " + value + "\n")
					}
				}
			}
		}
		//fmt.Println(keys)
		n += len(keys)
		if cursor == 0 {
			break
		}
	}
}

// 清除老的用户消息记录
func cleanMessageMembersData()  {
	// 需要匹配的前缀
	prefix := "m::message_"

	var cursor uint64
	var n int
	for {
		var keys []string
		var err error
		// 每次取出 10个
		keys, cursor, err = client.Scan(cursor, prefix+"*", 10).Result()
		if err != nil {
			panic(err)
		}
		// 获取 2021-06-01
		month6, err := time.Parse("2006-01-02", "2021-11-05")
		if err != nil {
			panic(err)
		}
		// 判断到今天的秒数差
		monthDiff := time.Now().Unix() - month6.Unix()
		for _, value := range keys {
			if strings.HasPrefix(value, prefix) {
				// 截取 key 里面的时间
				dateStr := strings.Replace(value, prefix, "", -1)[0:10]
				// 解析时间,并且清除指定时间之前的数据
				if timeObj, err := time.Parse("2006-01-02", dateStr); err == nil {
					//os.Stdout.WriteString(value+"\n")
					//fmt.Println(time.Now().Unix() - timeObj.Unix(),monthDiff, dateStr)
					if time.Now().Unix()-timeObj.Unix() > monthDiff {
						client.Del(value)
						os.Stdout.WriteString("del: " + value + "\n")
					}
				}
			}
		}
		//fmt.Println(keys)
		n += len(keys)
		if cursor == 0 {
			break
		}
	}
}

func main() {
	cleanMessageMembersData()
}
